package com.abo.yaas.webservice.utils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;

import javax.persistence.EntityManager;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.abo.yaas.domain.Auction;
import com.abo.yaas.domain.Category;
import com.abo.yaas.domain.User;
import com.abo.yaas.webservice.CriteriaQueryUtil;
import com.abo.yaas.webservice.hibernate.EMF;
import com.abo.yaas.webservice.hibernate.EntityManagerUtils;
import com.abo.yaas.webservice.hibernate.SortCriteria;

public class FixtureCreator implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        final EntityManager em = EMF.get().createEntityManager();
        EntityManagerUtils.setEntityManager(em);
        createCategories();
        createUsers();
        createAuctions();
    }

    private void createCategories() {
        long auctionCount = (CriteriaQueryUtil.getEntityCount(null, Category.class)).getValue();
        if (auctionCount < 10) {;
            for (int i = 0; i < 10; ++i) {
                final Category c = new Category();
                c.setTitle("Category");
                CriteriaQueryUtil.saveEntity(c);
            }
        }
    }

    private void createAuctions() {
        long auctionCount = (CriteriaQueryUtil.getEntityCount(null, Auction.class)).getValue();
        if (auctionCount < 50) {
            final Category c = getFakeCategory();
            final User u = getFakeUser();
            for (int i = 0; i < 50; ++i) {
                final Auction a = new Auction();
                final StringBuilder b = new StringBuilder();
                a.setTitle(b.append("Test Auction ").append(i).toString());
                a.setCategory(c);
                a.setDescription("A very brief description of the good item");
                a.setOwner(u);
                a.setStartTime(new Date());
                a.setEndTime(new Date(a.getStartTime().getTime() + 10000000));
                a.setStartPrice(new BigDecimal(0));
                a.setMinimumBid(new BigDecimal(1));
                CriteriaQueryUtil.saveEntity(a);
            }
        }
    }

    private User getFakeUser() {
        final Object id = CriteriaQueryUtil.getEntityIdAt(0, null, new ArrayList<SortCriteria<User>>(), User.class)
                .getValue();
        return CriteriaQueryUtil.getEntity(id, User.class);
    }

    private Category getFakeCategory() {
        final Object id = CriteriaQueryUtil.getEntityIdAt(0, null, null, Category.class).getValue();
        return CriteriaQueryUtil.getEntity(id, Category.class);
    }

    private void createUsers() {
        long userCount = (CriteriaQueryUtil.getEntityCount(null, User.class)).getValue();
        if (userCount < 50) {
            for (int i = 0; i < 50; ++i) {
                final User u = new User();
                u.setFirstName("FirstNameUser");
                u.setLastName("LastNameUser");
                u.setLogin("TestUser" + i);
                u.setPassword("password123");
                u.setDuplicatePassword("password123");
                u.setIsAdmin(i == 0);
                UserFacade.register(u);
            }
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // NOP
    }
}
